﻿Imports MapWinGIS

<Serializable()> Public Class DWGShapeLayer
    Inherits Layer

    Protected textList As New List(Of FText)
    Dim entList As New List(Of FEntity)

    Public Sub New(ByVal ln As String, ByVal tList As List(Of FText), ByVal eList As List(Of FEntity))
        MyBase.new(ln, "")

        entList.AddRange(eList)
        textList.AddRange(tList)

    End Sub

    Protected Sub New( _
      ByVal info As System.Runtime.Serialization.SerializationInfo, _
      ByVal context As System.Runtime.Serialization.StreamingContext)

        MyBase.New(info, context)

    End Sub



    Public Overrides Sub Dispose()

        Dim sf As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)
        sf.Close()

    End Sub

    Dim m_iDraw As Integer = -1
    Protected Overrides Sub DrawText()
        mapwin.ClearDrawing(m_iDraw)
        mapwin.LayerFont(m_iLayer, m_TextFont.Name, m_TextFont.Size)

        If m_bShowText Then


            m_iDraw = mapwin.NewDrawing(MapWinGIS.tkDrawReferenceList.dlSpatiallyReferencedList)

            mapwin.set_DrawingLabelsScale(m_iDraw, True)

            Dim ucl As UInt32 = RGB(m_TextColor.R, m_TextColor.G, m_TextColor.B)


            For Each dbt As FText In textList

                Dim just As MapWinGIS.tkHJustification = tkHJustification.hjRaw

                Select Case dbt.justification

                    Case TEXTJUSTIFICATION.CENTER
                        just = tkHJustification.hjCenter

                    Case TEXTJUSTIFICATION.LEFT
                        just = tkHJustification.hjLeft

                    Case TEXTJUSTIFICATION.RIGHT
                        just = tkHJustification.hjRight


                End Select

                mapwin.AddDrawingLabel(m_iDraw, dbt.str, ucl, dbt.x, dbt.y, just)

            Next


        End If

     
    End Sub

    Public Overrides Sub InitLayer(ByRef m As AxMapWinGIS.AxMap)
        mapwin = m
       
      


        '还好拥有共同的特征
        Dim sf As New MapWinGIS.Shapefile()
        sf.Open(fname)
        m_iLayer = mapwin.AddLayer(sf, True)

        mapwin.set_LayerKey(m_iLayer, lname)








        '现在可以更新自己的所有属性了
        InitProp()

        Me.DrawText()

    End Sub

    Public Overrides Sub InitNewLayer(ByRef m As AxMapWinGIS.AxMap, ByVal OutPath As String)
        Dim sf As New Shapefile
        Dim strPath(2) As String


       


        sf.CreateNew("", MapWinGIS.ShpfileType.SHP_POLYLINE)




        Dim f As New Field

        f = New Field
        f.Name = "Name"
        f.Type = FieldType.STRING_FIELD
        f.Width = 50

        Dim fd As New Field

        fd = New Field
        fd.Name = "desc"
        fd.Type = FieldType.STRING_FIELD
        fd.Width = 50


        sf.StartEditingShapes(True)
        sf.EditInsertField(f, 0)
        sf.EditInsertField(fd, 1)

        sf.StopEditingShapes(True, True)


        Me.AddEntityRange(entList, sf)






        '加入载入的逻辑

        Dim strSave As String = OutPath + lname + ".shp"

        strPath(0) = OutPath + lname + ".shp"
        strPath(1) = OutPath + lname + ".dbf"
        strPath(2) = OutPath + lname + ".shx"

        For i As Integer = 0 To strPath.Length - 1
            If System.IO.File.Exists(strPath(i)) Then
                Try
                    System.IO.File.SetAttributes(strPath(i), IO.FileAttributes.Normal)
                    System.IO.File.Delete(strPath(i))
                Catch ex As Exception

                End Try

            End If
        Next

        Dim cb As New GCallback

        Dim bS As Boolean = sf.SaveAs(strSave, cb)

        If Not bS Then

            System.Windows.Forms.MessageBox.Show(lname + "文件创建失败！，请查看电脑软件使用权限")

        End If

        fname = strSave


        Me.InitLayer(m)



    End Sub

    Public Overrides Sub InitProp()

    End Sub

    Public Overrides Sub ReLoad(ByRef m As AxMapWinGIS.AxMap, ByVal fPath As String)
        fname = fPath + lname + ".shp"

        Me.InitLayer(m)
    End Sub

    Public Overrides Sub Save()
        Dim sf As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)
        sf.StartEditingShapes()
        Dim g As New GCallback
        Dim bS As Boolean = sf.Save(g)
        sf.StopEditingShapes()
        If bS Then

        Else


            System.Windows.Forms.MessageBox.Show("文件保存错误，请查看相应权限!")

        End If
    End Sub

    Public Overrides Sub SaveAs(ByVal strPath As String)

        Dim fn As String = strPath + lname + ".shp"

        Dim myPath(2) As String
        myPath(0) = strPath + lname + ".shp"
        myPath(1) = strPath + lname + ".dbf"
        myPath(2) = strPath + lname + ".shx"


        For i As Integer = 0 To myPath.Length - 1
            If System.IO.File.Exists(myPath(i)) Then
                Try
                    System.IO.File.SetAttributes(myPath(i), IO.FileAttributes.Normal)
                    System.IO.File.Delete(myPath(i))
                Catch ex As Exception

                End Try

            End If
        Next

        Dim sf As MapWinGIS.Shapefile = mapwin.get_GetObject(m_iLayer)

        Dim bS As Boolean = sf.SaveAs(fn)

        If Not bS Then
            System.Windows.Forms.MessageBox.Show("文件保存错误，请查看相应权限!")

        End If

        '这个时候文件句柄已经改变，所以需要取回原来的句柄
        Dim b As Boolean = sf.Open(fname)
        If Not b Then
            System.Windows.Forms.MessageBox.Show("回读文件失败，请不要删除或更改项目文件!")

        End If

    End Sub


    Public Sub AddEntityRange(ByVal lst As System.Collections.Generic.List(Of FEntity), ByRef sf As MapWinGIS.Shapefile)





        Dim ptlist As MapWinGIS.Point()
        Dim sn As String
        Dim desc As String

        Dim sfPoly As MapWinGIS.Shapefile = sf



        sfPoly.StartEditingShapes(True)



        For Each en As FEntity In lst



            sn = en.sn
            ptlist = en.ptlist
            desc = en.desc


            If ptlist.Length < 2 Then

            Else


                Dim shp As New Shape
                shp.ShapeType = ShpfileType.SHP_POLYLINE
                Dim ptIndex As Integer = 0

                Dim n As Integer = ptlist.Length



                For i As Integer = 1 To n

                    shp.InsertPoint(ptlist(i - 1), ptIndex)


                Next




                Dim shpIndex As Integer = sfPoly.NumShapes

                sfPoly.EditInsertShape(shp, shpIndex)

                sfPoly.EditCellValue(0, shpIndex, sn)
                sfPoly.EditCellValue(1, shpIndex, desc)





            End If





        Next

        sfPoly.StopEditingShapes()








    End Sub
End Class
